package org.bigmarketapi.projectreview.DesingMode.PipeLineMode;

import jakarta.annotation.PostConstruct;
import org.bigmarketapi.projectreview.DesingMode.PipeLineMode.handler.InputDataPreChecker;
import org.bigmarketapi.projectreview.DesingMode.PipeLineMode.handler.ModelInstanceCreator;
import org.bigmarketapi.projectreview.DesingMode.PipeLineMode.handler.ModelInstanceSaver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @BelongsProject: ProjectReview
 * @BelongsPackage: org.bigmarketapi.projectreview.DesingMode.PipeLineMode
 * @Author: dujiaqi
 * @CreateTime: 2024-12-14  22:20
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class PipelineRouteConfig {
    private ApplicationContext context;

    public PipelineRouteConfig(ApplicationContext context) {
        this.context = context;
    }
    private static final Map
            <Class<? extends PipelineContext>,
                    List<Class<? extends ContextHandler<? extends PipelineContext>>>>
            PIPELINE_ROUTE_MAP = new ConcurrentHashMap<>(4);

    @PostConstruct
    public void init() {
        PIPELINE_ROUTE_MAP.putIfAbsent(InstanceBuildContext.class, Arrays.asList(InputDataPreChecker.class, ModelInstanceCreator.class, ModelInstanceSaver.class));
    }

    @Bean
    public Map<Class<? extends PipelineContext>, List<? extends ContextHandler<? extends PipelineContext>>> getPipeHandlerMap() {
        return PIPELINE_ROUTE_MAP.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> {
          return    createPipe(entry.getValue());
        }));
    }

    private List<? extends ContextHandler<? extends PipelineContext>> createPipe(List<Class<? extends ContextHandler<? extends PipelineContext>>> value) {
        return value.stream().map(handlerCLass-> context.getBean(handlerCLass)).collect(Collectors.toList());
    }


}
